<template>
	<view class="main">
		<view class="top">
			<image src="http://jiaoyukehu.oss-cn-beijing.aliyuncs.com/static/login/icon01.png"
				style="height:200rpx;width:200rpx;"></image>
			<span class='tit'>{{title}}</span>
		</view>

		<button class='btn' open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手机快捷登录</button>

		<view class="radio" @click="setCheck()">
			<radio value="r1" color='#0ABA94' :checked="check" />已阅读并同意
			<span class="rd" @click.stop="toPrivacyAgreement(3)">{{protocol[0]}}</span>
			以及
			<span class="rd" @click.stop="toPrivacyAgreement(4)">{{protocol[1]}}</span>
		</view>
		<!-- <label class="radio"><radio value="r1" checked="true" color="#0ABA94"/>已阅读并同意
		<span class="rd">{{protocol[0]}}</span>以及<spans class="rd">{{protocol[1]}}</span></label> -->


	</view>
</template>

<script setup lang="ts">
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import {
		ref,
		getCurrentInstance
	} from 'vue';
	import type {
		Ref
	} from 'vue'
	import {
		hqCode
	} from '@/util/auth.js'

	const {
		proxy
	} = getCurrentInstance() as any
	const title = ref('找护士找护工 就上秒陪')
	const protocol = ref([
		'《秒陪隐私政策》',
		'《秒陪服务协议》'
	])

	let check = ref(false)

	let setCheck = () => {
		check.value = !check.value
		console.log('??', check)
	}

	function toLogin(loginStatus) {
		console.log(loginStatus, 'loginStatus2')
		if (check.value === true) {
			const phone_code =uni.getStorageSync('phone_code')
			if (loginStatus == true) {
				uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
					delta: 1
				});
			}else if(phone_code!=' '){
				
			}else {
				uni.navigateTo({
					url: '../login/loginVerify/loginVerify'
				});
			}
		} else {
			uni.showToast({
				title: "请勾选同意！"
			});
		}

		console.log('??', check)
	}

	function getPhoneNumber(res) { // 获取手机号
		// code: "a77c1cbf2e490fef97b9b82ef6fd122eff8520293d5f9fcde0d3144cf5bef939"
		// encryptedData: "qC/oJTSAxbVIVHX/g03Ke2iLhJyaR2wqzh9mSgdxkvzO7POi5N5Jm8cYEt9yv4PramNh+q1zC+bi0OBJj/Uj1r7rJoJxZ+Qm3yDGhuFmmY0eWqX2omZcc+cwDrjquE8TPX+ql3Yt7uREhPi9YsKX9QVY/Dto6dlpyJn4PZLgpy/XEDw5mu7yyMa521IZ/kNuJwH72rjs6LNm+i5vJPr9lA=="
		// errMsg: "getPhoneNumber:ok"
		// iv: "KXFZtGC4alcf54hAelGiAQ=="

		let phone_code = res.detail.code // 获得的手机code
		let phone_encryptedData = res.detail.encryptedData //用于解密
		let phone_iv = res.detail.iv // 用于解密
		let loginCode = uni.getStorageSync('loginCode')
		console.log(phone_code, loginCode, 'phone_code')
		// hqCode(phone_encryptedData,phone_iv)
		uni.setStorageSync('phone_code', phone_code)
		// 首次登錄
		login(phone_code, loginCode)
		return
	}

	function login(phone_code, loginCode) {
		getUserInfo()
		let code = '1'
		let url = proxy.$API.login
		let param = {
			phoneCode: phone_code,
			loginCode: loginCode
		}
		console.log(proxy.$API.login, 'login')
		proxy.$UNIHTTP.postData(url, param).then((res : any) => {
			if (res.code == 0) {
				// accessToken: "e307300af6ee41bda08a102379ce9330"
				// expiresTime: 1692349718687
				// refreshToken: "bbf6cb2251284398892f1d90babf39a2"
				// userId: 2
				uni.setStorageSync('loginInfo', res.data)
				toLogin(true)
			} else {
				toLogin(false)
				if (res.msg) {
					uni.showToast({
						title: res.msg, //显示的文字 
						icon: 'none'
					})
				}
			}
		})

	};

	function getUserInfo() {
		let url = proxy.$API.getUserInfo
		console.log(proxy.$API.getUserInfo, 'login')
		proxy.$UNIHTTP.getData(url, {}).then((res : any) => {
			if (res) {
				if (res.code == 0) {
					// dataList.userInfo = res.data
					uni.setStorageSync('userInfo', res.data)

				} else if (res.msg) {
					uni.showToast({
						title: res.msg, //显示的文字 
						icon: 'none'
					})
				}
			} else {
				uni.showToast({
					title: '系统错误', //显示的文字 
					icon: 'none'
				})
			}
		})
	};
	const toPrivacyAgreement = (id) => {
		uni.navigateTo({ url: `/pages/accompany-tmp/my/privacyAgreement/privacyAgreement?id=${id}` });
	}


	//測試
	// const getAPPID = () => {
	// 	 let code = '1'
	// 	 let url = proxy.$API.login
	// 	 let param = {
	// 	   phoneCode: "hello",
	// 	   loginCode: "word"
	// 	 }
	// 	 console.log(proxy,'proxy')
	// 	 console.log(proxy.$API.login,'captchaGen')
	// 		proxy.$UNIHTTP.postData(url, param).then((res: any) => {
	// 			if(res.code == 200){

	// 			}else if(res.msg){
	// 				uni.showToast({
	// 						title: res.msg,//显示的文字 
	// icon:'none'
	// 					})

	// 			}
	// 			// if (res.open_id) {
	// 			// 	local.setCookie('APP_ID', res.open_id)
	// 			// 	local.set('auth_token', res.token)
	// 			// 	local.set('APP_ID', res.open_id)
	// 			// 	// routeSwitch.value = true
	// 			// }
	// 		})
	// 	}


	onLoad(() => {
		hqCode()

		// test()
		// getData
		// getAPPID()
		// console.log(check,'check')

	})
</script>

<style lang="scss" scoped>
	.main {
		// height: 100%;
		// width: 100%;
	}

	.top {
		height: 500rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		.tit {
			margin-top: 20rpx;
			font-weight: bold;
			font-size: 28rpx;

		}

		// height: 88rpx;
	}

	.btn {
		margin: 0 auto;
		background-color: #0ABA94;
		color: #fff;
		font-size: 30rpx;
		width: 90%;
		border-radius: 40rpx;
	}

	.radio {
		display: flex;
		// flex-direction: column;
		align-items: center;
		justify-content: center;
		margin-top: 20rpx;
		font-size: 24rpx;

		.rd {
			color: #0ABA94;
		}
	}
</style>